package com.atguigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.SetmealMapper;
import com.atguigu.dao.SetmealTravelgroupMapper;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.pojo.Setmeal;
import com.atguigu.pojo.SetmealExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService{

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealTravelgroupMapper setmealTravelgroupMapper;

    @Override
    public void save(Setmeal setmeal, Integer[] travelgroupIds) {
        setmealMapper.insert(setmeal);
        setmealTravelgroupMapper.batchInsert(setmeal.getId(),travelgroupIds);
    }

    @Override
    public void delete(Integer id) {
        setmealMapper.deleteByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        SetmealExample setmealExample =new SetmealExample();
        SetmealExample.Criteria criteria = setmealExample.createCriteria();
        if (queryPageBean.getQueryString() !=null && queryPageBean.getQueryString() !=""){
            criteria.andCodeEqualTo(queryPageBean.getQueryString());
            setmealExample.or().andHelpcodeEqualTo(queryPageBean.getQueryString());
            setmealExample.or().andNameLike("%"+queryPageBean.getQueryString()+"%");
        }
        List<Setmeal> setmeals = setmealMapper.selectByExample(setmealExample);

        PageInfo<Setmeal> pageInfo = new PageInfo<Setmeal>(setmeals);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<Setmeal> getSetmealList() {
        return setmealMapper.selectByExample(null);
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public Setmeal getSetmealById(Integer id) {
        return setmealMapper.selectByPrimaryKey(id);
    }
}
